package com.webfinal.backsystem.Util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ImageUtil {
    public static String save(MultipartFile image){
        String originalFileName=image.getOriginalFilename();
        String saveToName=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String extension=originalFileName.substring(originalFileName.lastIndexOf(".")).toLowerCase();
        String osName=System.getProperty("os.name");
        String path="";
        if (osName.equals("linux")||osName.equals("Linux")){
            System.out.println("linux");
            path="/usr/share/nginx/html/image/";
            File imageFile=new File(path+saveToName+extension);
            try {
                image.transferTo(imageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("win");
            path="D:/Web项目实践/web_final_homework/web_final_homework/manage_web/vue-admin-template/image";
            File imageFile=new File(path+saveToName+extension);
            try {
                image.transferTo(imageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("/image/"+saveToName+extension);
        return "http://localhost:9528/image/"+saveToName+extension;
    }
}
